package com.shanyi.common.util;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class CsvUtil {
    public static List<String> getCsvHead(MultipartFile file) {


        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        try {
            inputStream = file.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream);
            reader = new BufferedReader(inputStreamReader);

            CSVParser parser = CSVFormat.DEFAULT.parse(reader);
            List<CSVRecord> list = parser.getRecords();
            int length = list.size();

            if (length > 0) {

                ArrayList<String> headList = new ArrayList<>();

                int rowIndex = 0;
                int size = list.get(0).size();

                for (CSVRecord record : list) {
                    // 处理表头
                    if (rowIndex == 0) {
                        for (int i = 0;i < size;i++){
                            headList.add(record.get(i));
                        }
                        rowIndex++;
                    }
                    // todo:返回内容数据
                    if (rowIndex > 0) {
                    }
                }
            }

            if (length == 0) {

            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // todo:关闭流
        }

        return null;
    }
}
